import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

enum CardImage{
	
	KNIGHT("knight"),
	BISHOP("bishop"),
	KING("king"),
	QUEEN("queen"),
	PREIST("preist"),
	CONSCRIPT("conscript"),
	PEASANT("peasant");
	
	CardImage(String s){
		cardName = s;
		try {
			cardImage = ImageIO.read(new File(cardName + ".png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Image getImage(){
		return cardImage;
	}
	
	public String toString(){
		return "cardImage=\"" + cardName + "\"";
	}
	public String getName(){
		System.out.println(cardName);
		return cardName;
	}
	
	private Image cardImage;
	private String cardName;
	
}
@SuppressWarnings("serial")
public class Card extends JComponent{
	Card(String id, CardImage imageType){
		flipped = false;
		try{
			backOfCard = ImageIO.read(new File("backOfCard.png"));
		} catch(IOException e){
			e.printStackTrace();
		}
		this.id = id;
		this.imageType = imageType;
		current = backOfCard;
		
		this.setPreferredSize(new Dimension(90, 130));		
	}
	Card(String save){
		try{
			backOfCard = ImageIO.read(new File("backOfCard.png"));
		} catch(IOException e){
			e.printStackTrace();
		}
		current = backOfCard;
		String s[] = save.split(" ");
		for(int i = 0; i < s.length;i++){
			if(s[i].contains("id")){
				String t[] = s[i].split("\"");
				id = t[1];
			} else if(s[i].contains("cardImage")){
				String t[] = s[i].split("\"");
				try {
					imageType = stringToCardImage(t[1]);
				} catch (CardTypeNotFoundException e) {	e.printStackTrace();}
			} else if(s[i].contains("flipped")){
				String t[] = s[i].split("\"");		
			}
		}

		this.setPreferredSize(new Dimension(90, 130));	
	}
	
	public void paintComponent(Graphics g){
		if(current == null) return;
		
		g.drawImage(current, 4, 4, 86, 126, this);
	}
	/**
	 * Flips the card from imageType to backOfCard;
	 */
	public void flip(){
		flipped = !flipped;
		if(flipped){
			current = imageType.getImage();
		} else {
			current = backOfCard;
		}
		repaint();
	}
	
	public CardImage getImageType(){
		return imageType;
	}
	
	public String getFace(){
		return imageType.getName();
	}
	public boolean equals(Object otherObj){
		
		if (otherObj == null) return false;
		
		if( getClass() != otherObj.getClass()) return false;
		
		Card other = (Card)otherObj;
		
		return other.getImageType() == imageType;
		
		
	}
	/**
	 * @return returns if the card is either flipped (showing it's face) 
	 * 			or not (showing it's back);
	 */
	public boolean isFlipped(){ return flipped; }
	
	/**
	 * Return the XML format of the card
	 */
	
	public static CardImage stringToCardImage(String s) throws CardTypeNotFoundException {
		s = s.toLowerCase();
		if(s.equals("knight")){
			return CardImage.KNIGHT;
		} 
		else if(s.equals("bishop")){
			return CardImage.BISHOP;
		}
		else if(s.equals("king")){
			return CardImage.KING;
		}
		else if(s.equals("queen")){
			return CardImage.QUEEN;
		}
		else if(s.equals("preist")){
			return CardImage.PREIST;
		}
		else if(s.equals("conscript")){
			return CardImage.CONSCRIPT;
		}
		else if(s.equals("peasant")){
			return CardImage.PEASANT;
		}
		else {
			throw new CardTypeNotFoundException();
		}
	}
	public String toString(){
		return "<Card id=\"" + String.valueOf(id) + "\" " + imageType.toString() + " flipped=\"" + String.valueOf(flipped) + "\" " +  " />" ;
	}
	
	// ID of the Card
	private String  id;
	
	private boolean flipped;
	
	private Image current;
	private CardImage imageType;
	private static Image backOfCard;
	
	
	/* Card Test
	public static void main(String[] args){
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1, 3));
		p.add(new Card("1", CardImage.PEASANT));
		p.add(new Card("2", CardImage.KING));
		p.add(new Card("3", CardImage.PREIST));
		
		JFrame f = new JFrame();
		f.add(p);
		f.setVisible(true);
		f.pack();
		
	}
	*/
}
